<template>
  <div>
    <!---搜索表单-->
    <div class="search-div">
      <el-form label-width="70px" size="small">
        <el-row>
          <el-col :span="12">
            <el-form-item label="关键字">
              <el-input
                v-model="form.username"
                style="width: 100%"
                placeholder="用户名"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="创建时间">
              <el-date-picker
                v-model="createTimes"
                type="datetimerange"
                range-separator="To"
                start-placeholder="开始时间"
                end-placeholder="结束时间"
                format="YYYY-MM-DD HH:mm:ss"
                value-format="YYYY-MM-DD HH:mm:ss"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row style="display:flex">
          <el-button type="primary" size="small" @click="searchSysUser">
            搜索
          </el-button>
          <el-button size="small" @click="resetData">重置</el-button>
        </el-row>
      </el-form>
    </div>

    <!--    <el-dialog v-model="dialogVisible" title="添加或修改" width="40%">-->
    <!--      <el-form label-width="120px">-->
    <!--        <el-form-item v-show="false">-->
    <!--          <el-input v-model="sysUser.id"/>-->
    <!--        </el-form-item>-->
    <!--        <el-form-item label="名称">-->
    <!--          <el-input v-model="sysUser.username" />-->
    <!--        </el-form-item>-->
    <!--        <el-form-item label="真实姓名">-->
    <!--          <el-input v-model="sysUser.realName" />-->
    <!--        </el-form-item>-->
    <!--        <el-form-item label="性别">-->
    <!--            <el-radio-group v-model="sysUser.sex">-->
    <!--              <el-radio label="0">男</el-radio>-->
    <!--              <el-radio label="1">女</el-radio>-->
    <!--            </el-radio-group>-->
    <!--        </el-form-item>-->
    <!--        &lt;!&ndash;        <el-form-item label="期限">&ndash;&gt;-->
    <!--        &lt;!&ndash;          <el-select&ndash;&gt;-->
    <!--        &lt;!&ndash;              v-model="sysUser.repaymentdate"&ndash;&gt;-->
    <!--        &lt;!&ndash;              placeholder="请选择"&ndash;&gt;-->
    <!--        &lt;!&ndash;          >&ndash;&gt;-->
    <!--        &lt;!&ndash;            <el-option value="请选择"/>&ndash;&gt;-->
    <!--        &lt;!&ndash;            <el-option value="3期"/>&ndash;&gt;-->
    <!--        &lt;!&ndash;            <el-option value="6期"/>&ndash;&gt;-->
    <!--        &lt;!&ndash;            <el-option value="9期"/>&ndash;&gt;-->
    <!--        &lt;!&ndash;            <el-option value="12期"/>&ndash;&gt;-->
    <!--        &lt;!&ndash;          </el-select>&ndash;&gt;-->
    <!--        &lt;!&ndash;        </el-form-item>&ndash;&gt;-->
    <!--        <el-form-item label="logo">-->
    <!--          <el-upload-->
    <!--              class="avatar-uploader"-->
    <!--              action="http://localhost:9010/admin/system/fileUpload"-->
    <!--              :show-file-list="false"-->
    <!--              :on-success="handleAvatarSuccess"-->
    <!--          >-->
    <!--            <img v-if="sysUser.avatar" :src="sysUser.avatar" class="avatar" style="width: 30px" />-->
    <!--            <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>-->
    <!--          </el-upload>-->
    <!--        </el-form-item>-->
    <!--        <el-form-item>-->
    <!--          <el-button type="primary" @click="submit">提交</el-button>-->
    <!--          <el-button @click="dialogVisible = false">取消</el-button>-->
    <!--        </el-form-item>-->
    <!--      </el-form>-->
    <!--    </el-dialog>-->

    <!---数据表格-->
    <el-table :data="list" style="width: 100%">
      <el-table-column prop="username" label="用户名" />
      <el-table-column prop="realName" label="真实姓名" />
      <el-table-column prop="sex" label="性别" #default="scope">
        {{ scope.row.sex == 0 ? '男' : '女' }}
      </el-table-column>
      <el-table-column prop="avatar" label="头像" #default="scope">
        <img :src="scope.row.avatar" width="30" />
      </el-table-column>
      <el-table-column label="操作" align="center" width="280" #default="scope">
        <el-button type="danger" size="small" @click="deleteById(scope.row)">
          删除
        </el-button>
      </el-table-column>
    </el-table>

    <!--分页条-->
    <el-pagination
      v-model:current-page="pageParamsList.page"
      v-model:page-size="pageParamsList.limit"
      :page-sizes="[2, 20, 50, 100]"
      @size-change="fetchSysUser"
      @current-change="fetchSysUser"
      layout="total, sizes, prev, pager, next"
      :total="total"
    />
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'

import { ElMessage } from 'element-plus'
import { ElMessageBox } from 'element-plus'

import { useApp } from '@/pinia/modules/app'
import { getDeleteUser, getUser } from '@/api/user'

//重置
const resetData = () => {
  location.reload()
}

// const headers = {
//   token: useApp().authorization.token, // 从pinia中获取token，在进行文件上传的时候将token设置到请求头中
// }

const dialogVisible = ref(false)

//搜索方法
const searchSysUser = () => {
  fetchSysUser(
    pageParamsUsers.value.page,
    pageParamsUsers.value.limit,
    form.value
  )
}
// const sysUsers = {
//   id:'',
//   username:'',
//   realName:'',
//   sex:'',
//   avatar:''
// }
// const sysUser = ref(sysUsers)

// const submit = async () => {
//     const {code} = await getPostUser (sysUser.value);
//     if (code === 200) {
//       dialogVisible.value = false
//       ElMessage.success('操作成功')
//       fetchSysUser()
//     }
// }

//  图像上传成功以后的事件处理函数
// const handleAvatarSuccess = async (response, uploadFile) => {
//   sysUsers.remark = response.data
// }

// const addShow = async () => {
//   sysUser.value = ref('')
//   dialogVisible.value = true
// }

const queryDto = {
  username: '',
  createTimeBegin: '',
  createTimeEnd: '',
}
const form = ref(queryDto)

// const editShow = async row => {
//   sysUser.value = row
//   dialogVisible.value = true
// }
//开始和结束时间数据模型
const createTimes = ref([])

const list = ref([])
const fetchSysUser = async () => {
  //获取开始和结束时间
  if (createTimes.value.length == 2) {
    form.value.createTimeBegin = createTimes.value[0]
    form.value.createTimeEnd = createTimes.value[1]
  }
  const { data } = await getUser(
    pageParamsUsers.value.page,
    pageParamsUsers.value.limit,
    form.value
  )
  list.value = data.records
  total.value = data.total
}
onMounted(fetchSysUser)
//分页数据
const pageParamsList = {
  page: 1,
  limit: 10,
}
const total = ref(0)
const pageParamsUsers = ref(pageParamsList)

// 删除数据
const deleteById = row => {
  ElMessageBox.confirm('此操作将永久删除该记录, 是否继续?', 'Warning', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(async () => {
    const { code } = await getDeleteUser(row.lid)
    if (code === 200) {
      ElMessage.success('删除成功')
      pageParamsUsers.value.page = 1
      fetchSysUser()
    }
  })
}
</script>

<style scoped>
.demo-fit {
  display: flex;
  text-align: center;
  justify-content: space-between;
}
.demo-fit .block {
  flex: 1;
  display: flex;
  flex-direction: column;
  flex-grow: 0;
}

.demo-fit .title {
  margin-bottom: 10px;
  font-size: 14px;
  color: var(--el-text-color-secondary);
}
</style>
